package com.jasmine.bjstkserver.server.picture;

import com.jasmine.auth.util.AuthUtil;
import com.jasmine.bjstkserver.server.picture.model.PictureReq;
import com.jasmine.core.dto.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 照片
 * @author : jasmineXz
 */
@RestController
@RequestMapping("/picture")
public class PictureController {

    @Autowired
    private PictureService pictureService;


    /**
     * 查询照片列表
     */
    @GetMapping
    public R findList(@ModelAttribute PictureReq pictureReq) {
        pictureReq.setUserId(AuthUtil.getUserId());
        return R.ok(pictureService.findList(pictureReq));
    }


    /**
     * 上传照片,若 folderId 不为空,则上传至指定 folder
     * @param file 文件
     * @param uploadPath 上传路径
     * @param folderId 文件夹ID
     */
    @PostMapping("/upload")
    public R uploadPhoto(
            @RequestParam("file") MultipartFile file,
            @RequestParam(value = "uploadPath", required = false) String uploadPath,
            @RequestParam(value = "folderId", required = false) Integer folderId) {
        return R.ok(pictureService.insert(file,uploadPath,folderId));
    }


    /**
     * 上传照片,若 folderId 不为空,则上传至指定 folder
     * @param url 文件
     */
    @PostMapping("/download")
    public R download(@RequestParam("url") String url) {
        return R.ok();
    }


    /**
     * 修改照片信息,名称,喜欢等
     */
    @PutMapping
    public R update(@RequestBody PictureReq pictureReq) throws Exception {
        return R.ok(pictureService.update(pictureReq));
    }


    /**
     * 批量修改文件的文件夹
     */
    @PutMapping("/change/folder")
    public R changeFolder(@RequestBody PictureReq pictureReq) {
        pictureReq.setUserId(AuthUtil.getUserId());
        return R.ok(pictureService.changeFolder(pictureReq));
    }


    /**
     * 删除文件,将异步删除OS文件
     * @param pictureReq 文件ID/文件路径
     * @return 影响行数
     */
    @DeleteMapping
    public R delete(@RequestBody PictureReq pictureReq) {
        return R.ok(pictureService.delete(pictureReq.getPictureId(),pictureReq.getUrl()));
    }




    /* ------------------- 统计 ------------------- */




    /**
     * 统计文件数量和文件大小
     */
    @GetMapping("/statistics/all")
    public R statistics (PictureReq PictureReq) {
        PictureReq.setUserId(AuthUtil.getUserId());
        return R.ok(pictureService.statistics(PictureReq));
    }
}
